var tableID = "tableFlowNode";
var itemno = "";
var itemname = "";
var approveflow = '';
var flownumber = '';

$(document).ready(function() {
	itemno = getUrlParam("itemno");
	var data = parent.getData(itemno);
	itemname = data["itemname"];
	approveflow = data["approveflow"];
	
	$('[name="itemno"]').text(itemno);
	$('[name="itemname"]').text(itemname);

	initForm();
	
    $('#flownumber').select2({  
        placeholder: "请选择业务流程",  
        width:'100%'  
    });  
    
    $('#flownumber').change(function(){
    	var flownumber1 = $(this).val();
    	if(flownumber1 == null)
    		flownumber1 = flownumber;

    	getFlowData(flownumber1);
    })
    
});  

function initForm(){
	if(approveflow != ''){
		$.ajax({ 
			url: projectPath + "flow/getFlowBusinessInfo.do",
			data: {"businessnumber": approveflow},
			dataType: "json",
			success: function(data){
				if(data.result == 1){
					flownumber = data.datas[0].flownumber;

//					$('#flownumber').val(flownumber).trigger("change");
					bindFlow("flownumber", flownumber);
				}
	    	}
		});
	}
	else
		bindFlow("flownumber", null);
}

function getFlowData(flownumber1){
	$('#' + tableID).find('tbody').html('');
	
	var businessnumber = '';
	if(flownumber1 == flownumber){
		businessnumber = approveflow;
	}
	
	
	$.ajax({ 
		url: projectPath + "flow/getnodes.do",
		data: {"flownumber": flownumber1, "businessnumber": businessnumber},
		dataType: "json",
		success: function(data){
			if(data.result == 1){
				for(var i = 0; i < data.datas.length; i++)
					bindFlowForm(data.datas[i]);

			    bindRole('role', data.datas, '02');
			    bindDictMore('book', 'DM_WSLB', data.datas);
			}
    	}
	});
}


//ajax获取字典数据
function bindDictMore(selectID, dictcode, selectdata){
	var infoUrl = projectPath + "dict/getDictDetail.do";
	
	$.ajax({ 
		url: infoUrl,
		data: {"dictcode": dictcode},
		dataType: "json",
		success: function(data){
			if(data.result == 1){   
				var dataStr = data.datas;
				
				// 先清空控件数据
				$('.' + selectID).empty(); 

				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].itemcode;
					var text = dataStr[i].itemname;
					
					var option = $("<option>").val(value).text(text); 
					$('.' + selectID).append(option); 
				}	
				
				

				//选中下拉列表的值
				if(selectdata != null){
					for (var i = 0; i < selectdata.length; i++) {
						if(undefined != selectdata[i].bookcode){
							var bookcode = selectdata[i].bookcode.split(',');
							var index = selectdata[i].serialnumber;
							
							
							$('#book_' + index).val(bookcode);
						}
					}	
				}
			}
		}
	});
}

//ajax获取角色数据
function bindRole(selectID, selectdata, roletype){
	var infoUrl = projectPath + "role/list.do";
	

	var flowData = {};
	flowData["pageNum"] = "1";
	flowData["pageSize"] = "100";
	
	
	$.ajax({ 
		method: 'post',
		url: infoUrl,
		data: JSON.stringify(flowData),
		dataType: "json",
    contentType: "application/json",
		success: function(data){
			if(data.total > 0){   
				var dataStr = data.rows;
				
				// 先清空控件数据
				$('.' + selectID).empty(); 

//				var option = $("<option>").val("").text(""); 
//				$('#' + selectID).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					if(dataStr[i].roletype == roletype || roletype == null){
						var value = dataStr[i].rolecode;
						var text = dataStr[i].rolename;
						
						var option = $("<option>").val(value).text(text); 
						$('.' + selectID).append(option); 
					}
				}

				//选中下拉列表的值
				if(selectdata != null){
					for (var i = 0; i < selectdata.length; i++) {
						if(undefined != selectdata[i].rolecode){
							var rolecode = selectdata[i].rolecode.split(',');
							var index = selectdata[i].serialnumber;
	
							$('#role_' + index).val(rolecode);
						}
					}	
				}
			}
	}
	});
}


function bindFlowForm(data){
	var index = data.serialnumber;
    
    var nodecode = data.nodecode;
    var nodename = data.nodename;
        	
	
	var trStr = '<tr id="tr_'+ index +'" class="defindData">'+
				'<form class="form-horizontal flowDefined" id="nodeForm_'+ index +'">'+
			    '    <td style="text-align: center;">'+
			    '    	<span class="serialnumber">'+ index +'</span>'+
			    '    </td>'+
			    '    <td style="text-align: center;">'+
			    '    	<span class="nodecode_'+index+'">'+ nodecode +'</span>'+
		        '	</td>'+
			    '    <td style="text-align: center;">'+
			    '    	<span class="nodename_'+index+'">'+ nodename +'</span>'+
			    '    </td>'+
			    '    <td style="text-align: center;">'+
			    '    	<input type="checkbox" id="isjointlysign_'+ index +'">'+
			    '    </td>'+
			    '    <td style="text-align: center;">'+
			    '    	<select class="combox role rolecode_'+index+'" id="role_'+ index +'" name="role_'+ index +'" multiple>   '+
                '		</select> '+
			    '    </td>'+
			    '    <td style="text-align: center;">'+
			    '    	<select class="combox book bookcode_'+index+'" id="book_'+ index +'" name="book_'+ index +'" multiple>   '+				        
			    '    </td>'+
			    '</form>'+
			    '</tr>';
	

	$('#' + tableID).find('tbody').append(trStr); 
	
	if(data['isjointlysign'] == 1){
		$('#isjointlysign_' + index).prop('checked', true);
	}
	else{
		$('#isjointlysign_' + index).prop('checked', false);
	}


    $('#role_' + index + '').select2({
		placeholder : "请选择角色",
		tags : true,
		createTag : function(decorated, params) {
			return null;
		},
		width : '200px'
	}); 

    $('#book_' + index + '').select2({
		placeholder : "请选择文书",
		tags : true,
		createTag : function(decorated, params) {
			return null;
		},
		width : '230px'
	}); 
}

function save(){
	var jaParam = [];
	$('.serialnumber').each(function(){
		var paramData = {};
		var serialnumber = $(this).html();
		var nodecode = $('.nodecode_' + serialnumber).html();
		var nodename = $('.nodename_' + serialnumber).html();

		var rolecode = "";
		var rolename = "";
		var role = $('#role_' + serialnumber).select2("data");
		for(var i = 0; i < role.length; i++){
			rolecode += role[i].id + ',';
			rolename += role[i].text + ',';
		}
		rolecode = rolecode.substring(0, rolecode.length - 1);
		rolename = rolename.substring(0, rolename.length - 1);

		var bookcode = "";
		var bookname = "";
		var book = $('#book_' + serialnumber).select2("data");
		for(var i = 0; i < book.length; i++){
			bookcode += book[i].id + ',';
			bookname += book[i].text + ',';
		}
		bookcode = bookcode.substring(0, bookcode.length - 1);
		bookname = bookname.substring(0, bookname.length - 1);

		var jsonParam = {};
		jsonParam["serialnumber"] = serialnumber;
		jsonParam["nodecode"] = nodecode;
		jsonParam["nodename"] = nodename;
		if(rolecode != ''){
			jsonParam["rolecode"] = rolecode;
			jsonParam["rolename"] = rolename;
		}
		if(bookcode != ''){
			jsonParam["bookcode"] = bookcode;
			jsonParam["bookname"] = bookname;
		}
		
		var isjointlysign = $('#isjointlysign_' + serialnumber).is(":checked"); 
		if(isjointlysign){
			jsonParam["isjointlysign"] = 1;
		}
		else {
			jsonParam["isjointlysign"] = 0;
		}
		
		jaParam.push(jsonParam);
	})
	
	var flowData = $('#flownumber').select2("data");
	
	var flowBusiness = {};
	flowBusiness['businesstype'] = "01";
	flowBusiness['projectno'] = itemno;
	flowBusiness['projectname'] = itemname;
	flowBusiness['flownumber'] = flowData[0].id;
	flowBusiness['flowname'] = flowData[0].text;
	
	var formData = {};
	formData["FlowBusiness"] = flowBusiness;
	formData["FlowNodeParam"] = jaParam;
	

	$.ajax({
		method : 'post',
		url : projectPath + "flow/saveflowbusiness.do",
		data : JSON.stringify(formData),
		dataType : "json",
		contentType : "application/json",
		success : function(data) {
			window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success,{
				onOk:function(v){
			    	if(data.result == 1){
			    		parent.refreshData();		//刷新数据
			    	} 
				}
			});	

		}
	});
}